using System.Collections.Generic;
using System.Linq;
using DDA;
using UnityEngine;
using UnityEngine.UIElements;
using YDExtendTool.Scripts;

namespace YDExtendTool.Editor.TreeDataCreator
{
    public class InspectorView : VisualElement
    {
        public new class UxmlFactory : UxmlFactory<InspectorView, UxmlTraits>{}

        public IMGUIContainer InspectorBar;

        public InspectorViewData ViewData;

        private UnityEditor.Editor _odinEditor;

        public InspectorView()
        {
            Init();
        }

        private void Init()
        {
            InspectorBar = new IMGUIContainer() { 
                name = "InspectorBar",
                style ={flexGrow = 1}
            };
            ViewData = InspectorViewData.Instance;
            

            _odinEditor = UnityEditor.Editor.CreateEditor(ViewData);
            InspectorBar.onGUIHandler += () =>
            {
                _odinEditor.OnInspectorGUI();
            };
            Add(InspectorBar);
        }
        
        public void UpdateViewData()
        {
            HashSet<TreeNodeBase> data = Enumerable.ToHashSet(TreeDataCreator.instance.View.selection.OfType<TreeNodeView>()
                    .Select(nv => nv.NodeData));
            ViewData.Data.Clear();
            foreach (var value in data)
            {
                ViewData.Data.Add(value);
            }
        }
    }
}

